DSC 40B – Theoretical Foundations of Data Science II
Problems tagged with "sorted structure"
Problem #008
Tags: theoretical lower bounds, sorted structure
Consider the same problem as above, except now you may assume that the array is sorted. What is the tight theoretical lower bound for the worst case time complexity of any algorithm which solves this problem?
Solution
Problem #065
Tags: theoretical lower bounds, sorted structure
Suppose
Part 1)
What is a tight theoretical lower bound for this problem, assuming that the array is unsorted? State your answer in asymptotic notation as a function of the number of elements in the array,
Solution
Since the array is in arbitrary order, we have to at least read all of the elements, taking
Part 2)
What is a tight theoretical lower bound for this problem, assuming that the array is sorted? State your answer in asymptotic notation as a function of the number of elements in the array,
Solution
We can do this in worst-case
There cannot be an algorithm that is better than
Problem #197
Tags: sorted structure
A rotated sorted array is an array that is the result of taking a sorted array and moving a contiguous section from the front of the array to the back of the array. For example, the array [5, 6, 7, 1, 2, 3, 4]
is a rotated sorted array: it is the result of taking the sorted array [1, 2, 3, 4, 5, 6, 7]
and moving the first 4 elements, [1, 2, 3, 4]
, to the back of the array. Sorted arrays are also rotated sorted arrays, technically speaking, since you can think of a sorted array as the result of taking the sorted array and moving the first 0 elements to the back.
The function below attempts to find the value of the minimum element in a rotated sorted array. It is given the array arr
and the indices start
and stop
which indicate the range of the array that should be searched. One line in the function has been left blank.
import math
def find_minimum_in_rotated_sorted(arr, start, stop):
"""
Searches arr[start:stop] for the smallest element.
Assumes arr is a rotated sorted array.
"""
if stop < start:
return arr[0]
if stop == start:
return arr[start]
mid = (start + stop) // 2
if ________________:
return arr[mid + 1]
if arr[start] < arr[mid]:
return find_minimum_in_rotated_sorted(arr, mid, stop)
else:
return find_minimum_in_rotated_sorted(arr, start, mid - 1)
What should be filled in the blank line to make this function work correctly?
Solution
5th option: arr[middle] > arr[middle+1]